﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace MSSQLDAL
{
    class DalCommon
    {
        public static DataSet GetListPrivilege(DataSet listDS, string user_id)
        {
            string Deal_URL = "";
            string Query_URL = "";
            DataTable userDT = null;
            string UserSQL = "select c.user_id,c.dept_id,b.role_id,a.master_id from sys_operator_role a, sys_dept_roles b, sys_user c "
                            + "where a.master_id=b.master_id and a.user_id=c.user_id and a.user_id='" + user_id + "'";

            foreach (DataRow row in listDS.Tables[0].Rows)
            {
                Query_URL = GetHerfAdress(row["QUERY_NAME"].ToString(), row["QUERYHREFINFO_P"].ToString());
                Deal_URL = GetHerfAdress(row["DEAL_NAME"].ToString(), row["HREFINFO_P"].ToString());

                //if (row["EXEC_STATE"].ToString() == "1")
                //    row["DEAL_NAME"] = Query_URL;
                //else
                {
                    if (row["EXECUTOR_TYPE"].ToString() == "0" && row["EXECUTOR_VALUE"].ToString() == user_id)
                    {
                        row["DEAL_NAME"] = Deal_URL;
                    }
                    else if (row["EXECUTOR_TYPE"].ToString() == "1")  //角色
                    {
                        if (userDT == null)
                            userDT = SqlHelper.ExecuteDataset(Conn.SqlConnStr, CommandType.Text, UserSQL).Tables[0];
                        row["DEAL_NAME"] = userDT.Select("role_id='" + row["EXECUTOR_VALUE"].ToString() + "'").Length > 0 ? Deal_URL : Query_URL;

                    }
                    else if (row["EXECUTOR_TYPE"].ToString() == "2") //部门
                    {
                        if (userDT == null)
                            userDT = SqlHelper.ExecuteDataset(Conn.SqlConnStr, CommandType.Text, UserSQL).Tables[0];
                        row["DEAL_NAME"] = userDT.Select("dept_id='" + row["EXECUTOR_VALUE"].ToString() + "'").Length > 0 ? Deal_URL : Query_URL;
                    }
                    else if (row["EXECUTOR_TYPE"].ToString() == "3") //部门岗位
                    {
                        if (userDT == null)
                            userDT = SqlHelper.ExecuteDataset(Conn.SqlConnStr, CommandType.Text, UserSQL).Tables[0];
                        row["DEAL_NAME"] = userDT.Select("master_id='" + row["EXECUTOR_VALUE"].ToString() + "'").Length > 0 ? Deal_URL : Query_URL;
                    }
                    else
                        row["DEAL_NAME"] = Query_URL;
                }
            }
            return listDS;

        }


        public static string GetHerfAdress(string herf, string param)
        {
            if (herf.IndexOf("?") >= 0)
            {
                return herf + "&" + param;
            }
            else
            {
                return herf + "?" + param;
            }
        }
    }
}
